package st0003;

import java.util.HashMap;
import java.util.Map;

class Solution {//无重复字符的最长子串
    public int lengthOfLongestSubstring(String s) {
        int maxlen =0;
        Map<Character, Integer> hashtable = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char nowchar=s.charAt(i);
            if (hashtable.containsKey(nowchar)) {
                int j=hashtable.get(nowchar)+1;//指向前方冲突字母的下一个
                int nowlen=hashtable.size();
                if(nowlen>maxlen){
                    maxlen=nowlen;
                }
                hashtable.clear();
                for (int m = j; m < i; m++) {
                    hashtable.put(s.charAt(m), m);
                }
            }
            hashtable.put(s.charAt(i), i);
        }
        int nowlen=hashtable.size();
        if(nowlen>maxlen){
            maxlen=nowlen;
        }
        return maxlen;
    }
    public static void main(String[] args) {
        Solution a = new Solution();
        String s ="dvdf";
        int result=a.lengthOfLongestSubstring(s);
        System.out.println(result);
    }
}
